package com.zhixiang.xaec.action.demo;


import javax.annotation.Resource;

import net.sf.json.JSONArray;
import net.sf.json.JsonConfig;
import net.sf.json.util.CycleDetectionStrategy;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Scope;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.stereotype.Controller;

import com.zhixiang.core.web.action.BaseAction;
import com.zhixiang.xaec.entity.demo.Book;
import com.zhixiang.xaec.entity.demo.Reader;
import com.zhixiang.xaec.service.demo.BookService;
import com.zhixiang.xaec.service.demo.ReaderService;


@Controller
@Scope("prototype")
public class ReaderAction extends BaseAction
{

    private static final long serialVersionUID = -4838841766319086183L;

    private Reader reader;

    @Resource(type = ReaderService.class)
    private ReaderService readerService;

    @Test
    public void test()
    {
        ApplicationContext ac = new ClassPathXmlApplicationContext("conf/app-context.xml");
        SessionFactory sessionFactory = (SessionFactory)ac.getBean("sessionFactory");
        Session session = sessionFactory.openSession();

        Transaction trx = session.beginTransaction();

        Reader r = new Reader();

        r.setName("Reader zhang");

        Book b1 = new Book();

        b1.setTitle("title1");

        Book b2 = new Book();

        b2.setTitle("title2");

        b1.setReader(r);
        b2.setReader(r);
        r.getBooks().add(b1);
        r.getBooks().add(b2);
        session.save(r);
        trx.commit();

        session.close();
    }

    @Test
    public void testDel()
    {
        ApplicationContext ac = new ClassPathXmlApplicationContext("conf/app-context.xml");
        ReaderService readerService = (ReaderService)ac.getBean("readerServiceImpl");
        readerService.remove(new Long[] {Long.valueOf(10)});
    }

    @Test
    public void testSave()
    {
        ApplicationContext ac = new ClassPathXmlApplicationContext("conf/app-context.xml");
        ReaderService readerService = (ReaderService)ac.getBean("readerServiceImpl");
        Reader r = new Reader();

        r.setName("zhang 长啊 ");
         r.setId(Long.valueOf(3));

        Book b1 = new Book();

        b1.setTitle("tit1111");
         b1.setId(Long.valueOf(5));
        Book b2 = new Book();

        b2.setTitle("tit2222");
         b2.setId(Long.valueOf(6));
        b1.setReader(r);
        b2.setReader(r);
        r.getBooks().add(b1);
        r.getBooks().add(b2);

        readerService.save(r);
    }

    @Test
    public void testSaveBook()
    {
        ApplicationContext ac = new ClassPathXmlApplicationContext("conf/app-context.xml");
        BookService rbookService = (BookService)ac.getBean("bookServiceImpl");
        Reader r = new Reader();

        r.setName("zhang San");

        Book b1 = new Book();

        b1.setTitle("title1");

        Book b2 = new Book();

        b2.setTitle("title2");

        // b1.setId(Long.valueOf(3));
        b1.setReader(r);
        b2.setReader(r);
        r.getBooks().add(b1);
        r.getBooks().add(b2);
        rbookService.save(b1);
        rbookService.save(b2);

    }

    public String list()
    {
//        JsonConfig jsonConfig = new JsonConfig();
//        jsonConfig.setCycleDetectionStrategy(CycleDetectionStrategy.LENIENT);//解决转json的死循环问题策略
//        JSONArray jsonArray = JSONArray.fromObject(args,jsonConfig);
        setResult(readerService.getAll());
        JsonConfig jsonConfig = new JsonConfig();  //建立配置文件
        jsonConfig.setIgnoreDefaultExcludes(false);  //设置默认忽略
//        jsonConfig.setExcludes(new String[]{"name", "meno"});  //此处是亮点，只要将所需忽略字段
        
//        jsonConfig.setPropertyExclusionClassMatcher(PropertyExclusionClassMatcher.DEFAULT.getMatch(Book.class, ));
        jsonConfig.setCycleDetectionStrategy(CycleDetectionStrategy.LENIENT);//解决转json的死循环问题策略
        JSONArray jsonArray = JSONArray.fromObject(readerService.getAll(),jsonConfig);
        System.out.println(jsonArray.toString());
//        Object obj = getResult();
        return SUCCESS;
    }

    public String save()
    {

        Reader r = new Reader();

        r.setName("zhang San");

        Book b1 = new Book();

        b1.setTitle("title1");

        Book b2 = new Book();

        b2.setTitle("title2");
        b1.setReader(r);
        b2.setReader(r);
        r.getBooks().add(b1);
        r.getBooks().add(b2);
        readerService.save(r);
        return SUCCESS;
    }

    public Reader getReader()
    {
        return reader;
    }

    public void setReader(Reader reader)
    {
        this.reader = reader;
    }

}
